//
//  RootViewController.m
//  BCMobileVideo
//
//  Created by David McGaffin on 1/25/10.
//  Copyright Brightcove, Inc. 2010. All rights reserved.
//

#import "Constants.h"
#import "RootViewController.h"
#import "BCMediaAPI.h"
#import "BCPlayer.h"

@implementation RootViewController

@synthesize videoController;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Playlists";

    BCMediaAPI *bc = [[BCMediaAPI alloc] initWithReadToken:kAPIKey];
    NSError *err;
    BCItemCollection *result = [[bc findAllPlaylists:&err] retain];
    
    if (!result) {
        NSString *errorString = [bc getErrorsAsString:err];
        NSLog(@"Error: %s", errorString);
    } else {
        playlistsArray = [[result items] retain];
    }    
    [bc release];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release anything that can be recreated in viewDidLoad or on demand.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [playlistsArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	BCPlaylist *playlist = [playlistsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = playlist.name;

    return cell;
}




// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    videoController.playlist = [playlistsArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:videoController animated:YES];

}



- (void)dealloc {
    [playlistsArray release];
    playlistsArray = nil;
    [super dealloc];
}


@end

